package com.imooc.provider;

import com.imooc.creator.DefaultDataSourceCreator;
import com.imooc.propterties.DataSourceProperty;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
public abstract class AbstractDynamicDataSourceProvider implements DynamicDataSourceProvider {

    @Setter(onMethod_ = @Autowired)
    private DefaultDataSourceCreator defaultDataSourceCreator;

    protected Map<String, DataSource> createDataSourceMap(Map<String, DataSourceProperty> propertyMap) {
        Map<String, DataSource> map = new HashMap<>();
        for (Map.Entry<String, DataSourceProperty> entry: propertyMap.entrySet()) {
            DataSource dataSource = defaultDataSourceCreator.createDataSource(entry.getValue());
            map.put(entry.getKey(), dataSource);
        }
        return map;
    }

}
